package com.yanghui.lingyue.functions.python

import com.icedata.sika.bridge.*
import com.icedata.sika.core.CommandProcessor
import com.icedata.sika.core.Sika
import com.icedata.sika.database.Column
import com.icedata.sika.database.DataNotFoundException
import com.icedata.sika.database.Table
import com.yanghui.lingyue.bot.Bot
import com.yanghui.lingyue.data.Command
import com.yanghui.lingyue.data.Operation
import com.yanghui.lingyue.functions.AbstractFunction
import com.yanghui.lingyue.utils.functionUtils.FunctionUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.*

@Suppress("BlockingMethodInNonBlockingContext")
object GetSatellitePosition : AbstractFunction() {
    override val name = "Satellite"

    override suspend fun parse(event: SikaEvent, sika: Sika): LinkedList<Command> {
        val command = LinkedList<Command>()
        if (event is SikaUserEvent) return command
        event as SikaGroupEvent
        if (!FunctionUtils.checkValid(event)) return command
        if (event.sendGroup.id !in enableGroup) return command
        if (!FunctionUtils.atBot(event)) return command

        val param = FunctionUtils.extractKeyword(event, "卫星")
        param?.let {
            command.add(Command(event, name, it))
        }
        return command
    }

    override suspend fun execute(command: Command, sika: Sika): CommandProcessor.Result {
        val operation = Operation(command)
        Bot.sikaCore.databaseInsert(operation)
        val param = command.param
        try {
            val satellite = Bot.sikaCore.databaseSelect<Satellite>(param)
            var line: String?
            val builder = StringBuilder()
            withContext(Dispatchers.IO) {
                val pr = Runtime.getRuntime().exec(
                    "python -u C:\\LingYue\\SatellitePosition.py ${satellite.getArgv()}"
                )
                val input = BufferedReader(InputStreamReader(pr.inputStream, "GBK"))
                while (input.readLine().also { line = it } != null) {
                    builder.append(line)
                }
            }
            val messageBuilder = SikaMessageChainBuilder()
            messageBuilder.add(SikaAtMessage(command.sender.id))
            messageBuilder.add(SikaPlainMessage("\n" + builder.toString().replace(";", "\n")))
            command.sendGroup?.sendMessage(messageBuilder)
        } catch (e: DataNotFoundException) {
            command.sendGroup?.sendMessage("没有找到该卫星")
            return CommandProcessor.Result.BLOCK
        }
        return CommandProcessor.Result.ADOPT
    }

    @Table("Satellite", "", "name")
    data class Satellite(
        @Column("name")
        var name: String = "NULL",
        @Column("line1")
        var line_1: String = "",
        @Column("line2")
        var line_2: String = ""
    ) {
        fun getArgv(): String {
            return "@" + name.replace(" ", "$") + "@" +
                    line_1.replace(" ", "$") + "@" +
                    line_2.replace(" ", "$")
        }
    }
}